package com.example.kevin.resp;

import com.example.kevin.exception.AppExceptionMsg;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 异常响应类
 *
 * @param <T>
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Resp<T> implements Serializable {
    private static final long serialVersionUID = 3746261565666865695L;

    private Integer code;

    private String message;

    private T data;

    /**
     * 成功返回
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Resp succ(Object data) {
        return new Resp(200, "执行成功", data);
    }

    public static <T> Resp succ() {
        return new Resp(200, "执行成功", null);
    }

    /**
     * 执行失败
     *
     * @param code
     * @param message
     * @param <T>
     * @return
     */
    public static <T> Resp error(Integer code, String message) {
        return new Resp(code, message, null);
    }

    public static <T> Resp error(AppExceptionMsg appException) {
        return new Resp(appException.getCode(), appException.getMess(), null);
    }
}
